﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace dzs.Web
{
    public partial class left : Web.Admin.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    LoadTree(user_id);
                    //addNodes(roles_id);
                    //psgl();
                    //zlgl();
                    //zlgly();
                }
               
            }
        }



        private void LoadTree(int user_id)
        {
            DataSet ds = new dzs.BLL.rights().GetAllRights(" user_id=" + user_id);

            StringBuilder str = new StringBuilder();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];

                if ((int)dr["parent_id"] == 0)
                {
                    string name = dr["rights_name"].ToString();
                    //string url = dr["rights_url"].ToString();
                    int rights_id = (int)dr["rights_id"];

                    str.Append(" <tr><td height='20' background='images/main_36.gif'><table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                    str.Append(" <tr><td height='20'><div align='center'><table height='21' border='0' cellpadding='0' cellspacing='0'> ");
                    str.Append(" <tr><td valign='middle' onclick='showsubmenu("+(i+1)+");'>");
                    str.Append(" <div align='center' class='STYLE1' onmouseover=\"this.style.cursor='hand'\"> ");
                    str.Append(name);
                    str.Append(" </div> ");
                    str.Append(" </td></tr> </table></div></td></tr></table></td></tr>  ");
                    str.Append(" <tr id='submenu"+(i+1)+"'> ");
                    str.Append(" <td align='center' valign='top'> ");

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        if ((int)row["parent_id"] == rights_id)
                        {
                            str.Append("&nbsp;<a href='" + row["rights_url"] + "' target='mainFrame'>" + row["rights_name"] + "</a></br>");
                        }
                    }

                    str.Append(" </td> ");
                    str.Append(" </tr>   ");

                }

            }
            
            this.Literal1.Text = str.ToString();

        }

        
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeView t = sender as TreeView;
            if (t != null)
            {
                for (int i = 0; i < t.Nodes.Count; i++)
                {
                    if (t.SelectedNode.Value == t.Nodes[i].Value)
                    {
                        if (t.SelectedNode.Expanded != null)
                            t.SelectedNode.Expanded = !t.SelectedNode.Expanded;
                        else
                            t.SelectedNode.Expanded = true;
                    }
                }
            }

        }
    }
}